<?php
namespace app\index\controller;


use app\admin\util\Response;
use app\index\model\DesignCommentModel;
use app\index\model\DesignImgModel;
use app\index\model\DesignLikeModel;
use app\index\model\DesignModel;
use app\index\validate\DesignValidate;
use think\Config;
use think\Request;
use think\Session;

class Design extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_model = new DesignModel();
        $this->_like_model = new DesignLikeModel();
        $this->_comment_model = new DesignCommentModel();
        $this->_validate = new DesignValidate();
        $this->_img_model = new DesignImgModel();
    }

    protected function _before_add()
    {
        $vo = $this->_model->where(['uid'=>$this->_user_id])->find();
        if($vo){
            $this->assign('vo',$vo);
            $this->assign('action',url('update'));
        }else{
            $this->assign('action',url('insert'));
        }
    }

    public function insert(){
        $param = [];
        $param['contacts'] = input('contacts','','htmlspecialchars');
        $param['mobile'] = input('mobile','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['address'] = input('address','','htmlspecialchars');
        $param['logo'] = input('logo','','htmlspecialchars');
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $imgs = input('img/a','','htmlspecialchars');
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $model = new DesignModel([],['after_insert'=>function(DesignModel $model)use($imgs){
                $data = [];
                if(is_array($imgs)) {
                    $imgs = array_reverse($imgs);
                    foreach ($imgs as $value) {
                        $temp = [];
                        $temp['message_id'] = $model->getLastInsID();
                        $temp['path'] = $value;
                        $data[] = $temp;
                    }
                }
                $model->imgs()->saveAll($data);
            }]);
            $result = $model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
    public function update(){
        $param = [];
        $param['id'] = input('id',0,'intval');
        $param['contacts'] = input('contacts','','htmlspecialchars');
        $param['mobile'] = input('mobile','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['address'] = input('address','','htmlspecialchars');
        $param['logo'] = input('logo','','htmlspecialchars');
        $imgs = input('img/a','','htmlspecialchars');
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $id = $param['id'];
            unset($param['id']);
            $this->_model->isUpdate(true)->save($param,['id'=>$id]);
            $this->_img_model->where(['message_id'=>$id])->delete();
            $data = [];
            if(is_array($imgs)){
                $imgs = array_reverse($imgs);
                foreach ($imgs as $value){
                    $temp = [];
                    $temp['message_id'] = $id;
                    $temp['path'] = $value;
                    $data[] = $temp;
                }
            }
            $this->_img_model->saveAll($data);
            Response::json(1,'更新成功');
        }
    }
}
